<!--病例列表-表格-->
<template>
<div>
    <el-table
            :data="pageData"
            stripe
            style="width: 100%"
            @row-dblclick="handleView"
            v-loading="deleting"
    >
        <el-table-column
                label=""
                min-width="70">
            <template scope="scope">
                <el-button @click="handleView(scope.row,scope.$index)" size="small" >查看</el-button>
            </template>
        </el-table-column>
        <el-table-column
                prop="userRoleInfoDTO.patientInfo.name"
                label="患者"
                min-width="60"
                v-if="!showName">
        </el-table-column>
		<el-table-column
                prop="property.admissionAge"
                label="年龄"
                min-width="60">
        </el-table-column>
         <el-table-column
                label="诊断"
				min-width="170">
				<template slot-scope="scope">
					{{scope.row.property && scope.row.property.chiefComplaint}}
				</template>
				<!-- <el-table-column
                prop="property.chiefComplaint"
                label="诊断"
				min-width="170"
                :formatter="nameFormat"> -->
        </el-table-column>
        <el-table-column
                prop="userRoleInfoDTO.patientInfo.sex"
                label="性别"
                min-width="50"
                v-if="!showName">
        </el-table-column>
        
        <el-table-column
                :formatter="dateFormat"
                label="入院时间"
                min-width="90">
				<template slot-scope="scope">
					{{scope.row.property && dateFormat(scope.row.property.admissionDate)}}
				</template>
        </el-table-column>
        <el-table-column
                prop="property.firstSurgeryDate"
                label="手术时间"
                min-width="90">
				<template slot-scope="scope">
					<!-- {{scope.row}} -->
					{{scope.row.property && dateFormat(scope.row.property.firstSurgeryDate)}}
				</template>
        </el-table-column>
        <el-table-column
                label="出院时间"
                min-width="90">
				<template slot-scope="scope">
					{{scope.row.property && dateFormat(scope.row.property.dischargeDate)}}
				</template>
        </el-table-column>
        <el-table-column
                prop="creatorDoctorName"
                label="创建医生"
                min-width="70">
        </el-table-column>
        <el-table-column
                prop="supervisorDoctorName"
                label="上级医生"
                min-width="70">
        </el-table-column>
    </el-table>
</div>
</template>
<script>
import times from "winged/utils/wt-time-utils";
import gender from "winged/utils/wt-form-utils";
import Permission from "winged/utils/wt-permission";
export default {
	name: "wt-case-table",
	props: {
		list: {
			type: Array,
			required: true
		},
		showName: {
			type: Boolean,
			default: true
		}
	},

	data() {
		return {
			// 当前页的列表数据
			pageData: this.list,
			deleting: false,
			allowEdit: Permission.checkPermission("patient-case-add"),
			allowRemove: Permission.checkPermission("patient-case-delete")
			// 当前的filter
		};
	},
	watch: {
		list: function() {
			this.pageData = this.list;
		}
	},
	methods: {
		dateFormat: function(row, column) {
			// var date;
			// if (column.property.indexOf(".") > 0) {
			// 	if (
			// 		column.label == "入院时间" &&
			// 		row.properties.hospitalize_time != undefined
			// 	) {
			// 		date = row.properties.hospitalize_time;
			// 	} else if (
			// 		column.label == "出院时间" &&
			// 		row.properties.discharge_time != undefined
			// 	) {
			// 		date = row.properties.discharge_time;
			// 	}
			// } else {
			// 	date = row[column.property];
			// }
			// if (date == undefined) {
			// 	return "";
			// }

			if (column) {
				console.log("column");
				if (typeof row == "object" && row.property) {
					let date = Number(row[column.property]);
					if (date) {
						return times.format(date, "date");
					}
				}
				return "";
			}
			if (row) {
				//当作为一个工具函数使用时，row表示时间戳，无需传column
				return times.format(Number(row), "date");
			}
		},
		dateSurgery: function(row, column) {
			var date;
			var surgery = [];
			if (row.properties.surgery_time !== undefined) {
				var surgery_date = row.properties.surgery_time;
				surgery_date.forEach(function(r) {
					date = times.format(r, "date");
					surgery.push(date);
				});
				return surgery.join("\n/\n");
			}
		},
		showsix: function(row, column) {
			var six = row[column.property];
			return gender.formatGender(six);
		},
		handleView(row, index) {
			this.$router.push({ path: `/case/detail/${row.id}` });
			//window.location.href = "/admin/patients/case/" + row.id;
		},
		nameFormat: function(row, column) {
			var arr = row[column.property].split(",");
			var str = arr.join("").replace(/\""/g, " / ");
			var str1 = str.replace(/\"/g, "");
			return str1.replace("[", "").replace("]", "");
		},
		handleDelete: function(row, index) {
			var name = row.patientName;
			var arr = row.caseName.split(",");
			var str = arr.join("").replace(/\""/g, " / ");
			var str1 = str.replace(/\"/g, "");
			var vm = this;
			this.$confirm(
				'您将删除患者"' +
					name +
					'"的病例"' +
					str1.replace("[", "").replace("]", "") +
					'"，是否确定？',
				"操作提示",
				{
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}
			)
				.then(() => {
					this.deleting = true;
					vm.pageData.splice(index, 1);
					require("winged/utils/axios");
					let path = `patients/case/${row.id}`;
					window.axios.delete(path).then(function(response) {
						vm.deleting = false;
						if (response.status == 200) {
							vm.$message({
								type: "success",
								message: "删除成功!"
							});
						} else {
							vm.$message({
								ype: "error",
								message: "删除失敗!"
							});
						}
					});
				})
				.catch(() => {
					vm.deleting = false;
				});
		},
		handedit: function(row) {
			window.location.href = `/admin/patients/case/${row.id}/edit`;
		}
	},
	created() {
		console.log(this.list);
	}
};
</script>
